//
//  MyPayViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/21.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "MyPayViewController.h"
#import "AddCardViewController.h"
#import "ChooseCardViewController.h"


@interface MyPayViewController ()<UIAlertViewDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconUrl;
@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (weak, nonatomic) IBOutlet UILabel *cardNo;
@property (weak, nonatomic) IBOutlet UIImageView *backImage;

@property (weak, nonatomic) IBOutlet UITextField *payNumTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UILabel *bankType;

@end

@implementation MyPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _backImage.layer.cornerRadius = 22.5;
    _backImage.layer.masksToBounds = YES;
    self.navigationItem.title = @"我要充值";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    self.payNumTextField.delegate = self;
    self.pwdTextField.delegate = self;
    
       
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}
- (IBAction)addCard:(id)sender {
    
    AddCardViewController *addCardView = [[AddCardViewController alloc] init];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:addCardView animated:YES];
   
    addCardView.BLOCK = ^(MyBankCardModel *model){
        
        _bankName.text = model.BankName;
        _bankType.text = model.BankType;
        _cardNo.text = model.CardNo;
        
        NSString *url = [[model.IconUrl substringFromIndex:1] stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];//截取掉下标7之后的字符串
        [_iconUrl sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BaseURL,url]]];

    };
}

//确认充值点击事件
- (IBAction)clickPayBtn:(UIButton *)sender {
    
    if ([self.payNumTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入金额" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    if ([self.pwdTextField.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入交易密码" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    NSString *loginName = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginName"];
    NSDictionary *dict = @{
                           @"LoginName":loginName,
                           @"Dedit":self.payNumTextField.text,
                           @"BankCode":@"中国工商银行"
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Money.AddConfirmprepaid" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
        [self FengFu:[object[@"Data"] firstObject][@"WaterNumber"]];
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
    
    
}

- (void)FengFu:(NSString *)requestId {
    
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSString *subledgerList = [NSString stringWithFormat:@"[{\"roleType\":\"0\",\"roleCode\":\"%@\",\"inOrOut\":\"0\",\"sum\":\"%@\",\"bizFlag\":\"0\"}]", loginId, self.payNumTextField.text];
    NSDictionary *dic = @{
                          @"requestType":@"PFT0001",
                          @"requestId":requestId,
                          @"merchantCode":FengFuMerchantCode,
                          @"userIdIdentity":loginId,
                          @"sum":self.payNumTextField.text,
                          @"payType":@"1",
                          @"subledgerList":subledgerList,
                          @"mainAccountType":@"",
                          @"mainAccountCode":@"",
                          @"passThrough":@"",
                          @"noticeUrl":@"www.baidu.com",
                          @"successReturnUrl":@"",
                          @"failReturnUrl":@"",
                          };
    NSString *sign = [[[[[[[[[[[[dic[@"requestType"] stringByAppendingString:dic[@"requestId"]] stringByAppendingString:dic[@"merchantCode"]] stringByAppendingString:dic[@"userIdIdentity"]] stringByAppendingString:dic[@"sum"]] stringByAppendingString:dic[@"payType"]] stringByAppendingString:dic[@"subledgerList"]] stringByAppendingString:dic[@"mainAccountType"]] stringByAppendingString:dic[@"mainAccountCode"]] stringByAppendingString:dic[@"passThrough"]] stringByAppendingString:dic[@"noticeUrl"]] stringByAppendingString:dic[@"successReturnUrl"]] stringByAppendingString:dic[@"failReturnUrl"]];
    NSString *signature = [HMACMD5 HMACMD5WithString:sign WithKey:FengFuKEY];
    NSDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:dic];
    [dict setValue:signature forKey:@"signature"];
    [SumaPaySDK startService:dict callback:^(id resultDic, NSError *error) {
        NSLog(@"%@--------------%@", resultDic, error);
        NSDictionary *dic = [NetWorkManager dictionaryWithJsonString:resultDic];
        if ([dic[@"result"] integerValue] == 0) {
            if ([dic[@"msg"][@"result"] integerValue] == 00000) {
                [self Confirmprepaid:requestId];
            }
        }
    }];
}

- (void)Confirmprepaid:(NSString *)requestId {
    
    NSString *loginName = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginName"];
    NSDictionary *dict = @{
                           @"LoginName":loginName,
                           @"Dedit":self.payNumTextField.text,
                           @"WaterNumber":requestId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Money.Confirmprepaid" withParaments:dict withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@", object);
    } withFailureBlock:^(NSError *error) {
        
    } progress:^(float progress) {
        
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 100000) {
        self.payNumTextField.text = @"";
        self.pwdTextField.text = @"";
        [self.pwdTextField resignFirstResponder];
        [self.payNumTextField becomeFirstResponder];
    }
}

- (IBAction)clickBtn:(id)sender {
    
    ChooseCardViewController *choose = [[ChooseCardViewController alloc] init];
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:choose animated:YES];
    [self setHidesBottomBarWhenPushed:NO];
}

@end
